package kr.saeildamil.androidforstudy2.ch23;

import kr.saeildamil.androidforstudy2.R;
import android.app.Activity;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.ImageView;

public class CustomAnimation extends Activity {
	ImageView mAniTarget;
	Button mBtnAnim;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch23_customanimation);
		
		mAniTarget = (ImageView) findViewById(R.id.ch23_customanimation_animtarget);
	}
	
	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.ch23_customanimation_skewbtn:
			mAniTarget.startAnimation(new SkewAnimation());
			break;
		case R.id.ch23_customanimation_camerabtn:
			mAniTarget.startAnimation(new CameraAnimation());
			break;
		}
	}
	
	class SkewAnimation extends Animation{
		
		@Override
		public void initialize(int width, int height, int parentWidth,
				int parentHeight) {
			super.initialize(width, height, parentWidth, parentHeight);
			setDuration(1000);
			setInterpolator(new LinearInterpolator());
		}
		
		@Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			Matrix matrix = t.getMatrix();
			matrix.setSkew(0.5f * interpolatedTime, 0);
		}
	}
	
	class CameraAnimation extends Animation{
		float camerax, cameray;
		
		@Override
		public void initialize(int width, int height, int parentWidth,
				int parentHeight) {
			super.initialize(width, height, parentWidth, parentHeight);
			camerax = width/2;
			cameray = height/2;
			setDuration(1000);
			setInterpolator(new LinearInterpolator());
		}
		
		@Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			Camera cam = new Camera();
			cam.rotateY(360 * interpolatedTime);
			Matrix matrix = t.getMatrix();
			cam.getMatrix(matrix);
			matrix.preTranslate(-camerax, -cameray);
			matrix.postTranslate(camerax, cameray);
		}
	}
}
